package com.customerFilter;

import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import com.ql.util.express.IExpressContext;

/**
 * @version 0.1
 * @describe: 客户过滤器
 * @Date: 2020/11/12
 */
public class QLExpress {

    private final static ExpressRunner instance = new ExpressRunner();

    static {
        try {
            instance.addOperatorWithAlias("小于", "<", "$1<$2");
            instance.addOperatorWithAlias("大于", ">", "$1>$2");
            instance.addOperatorWithAlias("大于等于", ">=", "$1>=$2");
            instance.addOperatorWithAlias("小于等于", "<=", "$1<=$2");
            instance.addOperatorWithAlias("等于", "==", "$1==$2");
            instance.addOperatorWithAlias("不等于", "!=", "$1!=$2");
            instance.addOperatorWithAlias("包含", "like", "$1like$2");
            instance.addOperatorWithAlias("如果", "if", null);
            instance.addOperatorWithAlias("则", "then", null);
            instance.addOperatorWithAlias("否则", "else", null);
            instance.addOperatorWithAlias("并且", "and", null);
            instance.addOperatorWithAlias("或者", "or", null);
            //客户性别
            instance.addFunctionOfClassMethod("getDictGender", FilterAttribute.class.getName(), "getDictGender", new String[]{FilterAttribute.class.getName()}, null);
            instance.addMacro("性别", "getDictGender(customer)");
            //注册日期
            instance.addFunctionOfClassMethod("getRegisterDate", FilterAttribute.class.getName(), "getRegisterDate", new String[]{FilterAttribute.class.getName()}, null);
            instance.addMacro("注册日期", "getRegisterDate(customer)");
            //注册资本
            instance.addFunctionOfClassMethod("getRegisterCapital", FilterAttribute.class.getName(), "getRegisterCapital", new String[]{FilterAttribute.class.getName()}, null);
            instance.addMacro("注册资本", "getRegisterCapital(customer)");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean filter(FilterAttribute customer, String express) {
        IExpressContext<String, Object> expressContext = new DefaultContext<String, Object>();
        expressContext.put("customer", customer);
        try {
            return (boolean) instance.execute(express, expressContext, null, false, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
